<?php
/**
 * @copyright	Copyright (C) 2012 Tam Viet Tech. All rights reserved.
 * 
 * @author		Ngo Duc Lien <liennd@gmail.com>
 * @author		Luong Thanh Binh <ltbinh@gmail.com>
 */

if (!defined('SERVER_ROOT')) exit('No direct script access allowed');?>
<?php

class Controller {
    public $view;
    public $model;

    protected $app_name ='';
    protected $module_name = '';

    function __construct($app,$module)
    {
        //Load custom functions
        $func = SERVER_ROOT . 'apps' . DS . $app . DS . 'functions.php';
        if (is_file($func))
        {
            @require_once($func);
        }

        $this->app_name =$app;
        $this->module_name = $module;

        $v = $app . '_View';
        $this->view = (class_exists($v)) ? new $v($app, $module) : new View($app,$module);
        //tạo View cụ thể (View con) nếu có cài đặt (nó thừa kế từ View cha ở trong thư mục libs)
        //ngược lại chỉ tạo View cha trong thư mục lib
        $this->_load_model($app, $module);
        $this->model->app_name = $app;
        $this->model->module_name = $module;
    }

    public function CREATE_MENU()
    {
        $MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $arr_menu = $this->model->SELECT_MENU(SITE_ROOT,$MA_CAN_BO);
        $v_result = '<div id="cssmenu"><ul>';
        while ($arr = $arr_menu->FetchRow()){
               $TEN_MENU=$arr['TEN_MENU'];
               $HTML_MENU_CON=$arr['HTML_MENU_CON'];
               if($HTML_MENU_CON !=''){
                  $v_result = $v_result .'<li class="has-sub"><a href="#"><span>'.$TEN_MENU.'</span></a>
                                              <ul>' .$HTML_MENU_CON. '</ul>
                                          </li>';
               }
           }
        $v_result= $v_result .'</ul></div>';
        return $v_result;
    }
    public function CHECK_QUYEN($P_APPS,$P_MODULES){
        @Session::init();
        $P_MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_VAL = $this->model->CHECK_QUYEN($P_APPS,$P_MODULES);
        if ($P_VAL!='CO_QUYEN'){
            header('location:' . SITE_ROOT . 'hethong/khongcoquyen');
            return FALSE;
        }
        return TRUE;
    }
    public function CREATE_NHAC_VIEC()
    {
        $MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $arr_nhacviec = $this->model->SELECT_NHAC_VIEC($MA_CAN_BO);
        $v_result='';
        $i=0;
        while ($arr = $arr_nhacviec->FetchRow()){
               $MA_LOAI_CONG_VIEC=$arr['MA_LOAI_CONG_VIEC'];
               $TEN_LOAI_CONG_VIEC=$arr['TEN_LOAI_CONG_VIEC'];
               $URL_NHAC_VIEC=SITE_ROOT.$arr['URL_NHAC_VIEC'];
               $SO_LUONG_CHO_THUC_HIEN=$arr['SO_LUONG_CHO_THUC_HIEN'];
               if($MA_LOAI_CONG_VIEC !=''){
                  if ($i==0){// cai dau tien thi them cai div
                  $i=1;
                   $v_result = $v_result .'<div class="grid_24"><div id="roles"><ul>';
                  }
                  $v_result = $v_result .'<li class="" data-menu="1" data-role="'.$MA_LOAI_CONG_VIEC.'">
                                              <a href="' .$URL_NHAC_VIEC. '">'.$TEN_LOAI_CONG_VIEC;
                   if($SO_LUONG_CHO_THUC_HIEN>0){
                                        $v_result = $v_result .'[<span class="count" style="color:red">'.$SO_LUONG_CHO_THUC_HIEN.'</span>]';
                   }
                  $v_result = $v_result .'</a>
                                         </li>';
               }
           }
        if ($i>0){ //neu ton tai cai dau tien thi dong cai div
            $v_result= $v_result .'</ul></div></div>';
        }
        return $v_result;
    }



    private function _load_model()
    {
        $path = 'apps'.DS . $this->app_name . DS .'modules'.DS . $this->module_name . DS . $this->module_name. '_Model.php';
        if (file_exists($path))
        {
            require $path;
            $model_name = $this->module_name . '_Model';
            $this->model = new $model_name;
            $this->model->db->debug = DEBUG_MODE;
        }
        else
        {
            $this->error(1);
        }
    }

    public static function check_login()/* đã sửa-> có sử dụng*/
    {
        session::init();
        $TAI_KHOAN = Session::get(TIEP_DAU_NGU_SESSION.'TAI_KHOAN');
        if ($TAI_KHOAN == NULL)
        {
            session::destroy();
            header('location:' . SITE_ROOT . 'login.php');
            return FALSE;
        }

        return TRUE;
    }

    public function check_permission($function_code, $app_name='')
    {
        @Session::init();
        $app_name = ($app_name != '') ? strtoupper($app_name) : strtoupper($this->app_name);
        $function_code =  $app_name . '::' . $function_code;
        return in_array($function_code, Session::get(TIEP_DAU_NGU_SESSION.'arr_function_code'));
    }

    public static function get_post_var($html_object_name, $defaul='', $is_replace_bad_char=TRUE)
    {
        $var = isset($_POST[$html_object_name]) ? $_POST[$html_object_name] : $defaul;

        if ($is_replace_bad_char)
        {
            return replace_bad_char ($var);
        }

        return $var;
    }

    public function error($error_code)
    {
        switch ($error_code)
        {
            case 1: //Ma loi 1: Không thấy file Model!
                die('Không thấy file Model!');
                break;
            case 2:
                die('xxx');
                break;
        }
    }

    public function access_denied()
    {
        die('Bạn không có quyền thực hiện chức năng này!');
    }
}